/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-10-04
 * Time: 16:34
 */
public class MyCircularQueue {

    public int [] elme;
    public int front; //记录队头
    public int rear;  //记录队尾

    // 初始化为 k 个元素得构造方法
    public MyCircularQueue(int k) {
        elme = new int[k];
    }

    //入队列
    public boolean enQueue(int value) {
        //满了不能在如
        if(isFull()){
            return false;
        }
        // rear 位置存放 value
        elme[rear] = value;
        // rear 向后走  % 让其循环
        rear = (rear+1) % elme.length;
        return true;

    }

    //出队列
    public boolean deQueue() {
        // 空队列
        if(isEmpty()) {
            return false;
        }
        front = (front+1) % elme.length;
        return true;
    }

    // 获取对头元素
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elme[front];
    }

    //获取队尾元素
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = rear == 0 ? elme.length -1 : rear-1;
        return elme[index];
    }

    public boolean isEmpty() {
        if(front == rear){
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if((rear+1) % elme.length == front){
            return true;
        }
        return false;

    }
}
